package com.wzl.common;

import com.wzl.common.constant.VkPublicConstants;
import com.wzl.common.errcode.VkErrorCode;
import com.wzl.common.errcode.VkErrorCodeEnum;
import com.wzl.common.exception.VkPublicException;
import lombok.Data;

import java.io.Serializable;
import java.util.Map;

/**
 * 接口返回类基类
 *
 * @author wzl
 * @since 2022-08-27 15:40
 */
@Data
public class VkPublicResponse implements Serializable {

    private static final long serialVersionUID = 1865981403129616196L;
    /**
     * 返回码
     */
    private String resultCode;
    /**
     * 返回描述
     */
    private String resultMsg;
    /**
     * 返回数据
     */
    private Object data;

    /**
     * 为后续业务发展需要，增加响应协议扩展字段
     */
    private Map<String, String> expandField;

    /**
     * 构造函数，设置默认值
     */
    public VkPublicResponse() {
        resultCode = VkPublicConstants.SUCCESS_CODE;
        resultMsg = VkPublicConstants.SUCCESS_MESSAGE;
    }

    public VkPublicResponse(VkErrorCodeEnum vkErrorCodeEnum) {
        this.resultCode = vkErrorCodeEnum.getCode();
        this.resultMsg = vkErrorCodeEnum.getMessage();
    }

    public VkPublicResponse(VkErrorCodeEnum info, String... args) {
        setResultInfo(info, args);
    }
    public VkPublicResponse(Object data) {
        this.data = data;
    }

    public VkPublicResponse(VkErrorCodeEnum vkErrorCodeEnum, Object data) {
        this.resultCode = vkErrorCodeEnum.getCode();
        this.resultMsg = vkErrorCodeEnum.getMessage();
        this.data = data;
    }

    public VkPublicResponse(String resultCode, String resultMsg) {
        setResultInfo(resultCode, resultMsg);
    }

    public void setResultInfo(String resultCode, String resultMsg) {
        this.resultCode = resultCode;
        this.resultMsg = resultMsg;
    }

    public VkPublicResponse(String resultCode, String resultMsg, Object data) {
        this.resultCode = resultCode;
        this.resultMsg = resultMsg;
        this.data = data;
    }

    public VkPublicResponse(VkPublicException e) {
        setResultInfo(e);
    }

    public void setResultInfo(VkErrorCode info) {
        if(info == null) {
            return;
        }
        this.resultCode = info.getCode();
        this.resultMsg = info.getMessage();
    }

    public void setResultInfo(VkErrorCodeEnum info, String... args) {
        this.resultCode = info.getCode();
        StringBuilder argsStringBuffer = new StringBuilder(",[");
        for (String arg : args) {
            argsStringBuffer.append(arg == null ? "null" : arg);
        }
        argsStringBuffer.append("]");
        this.resultMsg = info.getMessage() + argsStringBuffer;
    }

    public void setResultInfo(VkPublicException e) {
        if( e == null) {
            return;
        }
        this.resultCode = e.getErrCode();
        this.resultMsg = e.getErrMsg();
    }
}
